package org.example.memora.configur;

import jakarta.annotation.Resource;
import org.example.memora.interceptor.LoginInterceptor;
import org.example.memora.interceptor.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // token刷新拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns("/**")
                .excludePathPatterns(
                        //"/api/posts/**",  // todo:暂时放行
                       // "/api/users/**",
                        "/swagger-ui/**",
                        "/doc.html",
//                        "/ai/history/**",
//                        "/ai/chat",
//                        "/ai/chat/**",
                        "/v3/api-docs/**",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/favicon.ico",
                        "/error",
                        "/chat.html",
                        "/chat1.html",
                        "/user/**",
                        // 放行聊天页面
                        "/ws-chat/**",
                        // 放行 WebSocket
                        "/js/**", "/css/**"
                        //"/api/chat/history"
                )
                .order(0);

        // 登录校验拦截器
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/api/users/register",
                        "/api/users/login",
                        "/api/users/code",
//                        "/ai/history/**",
//                        "/ai/chat",
//                        "/ai/chat/**",
                        "/swagger-ui/**",
                        "/doc.html",
                        "/v3/api-docs/**",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/favicon.ico",
                        "/error",
                        "/chat.html",
                        "/chat1.html",
                        // 放行聊天页面
                        "/ws-chat/**",
                        "/user/**",
                        // 放行 WebSocket
                        "/js/**", "/css/**"
                       // "/api/chat/history"
                )
                .order(1);
    }
    // 跨域拦截器
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                // ✅ Spring Boot 3 推荐写法
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("Content-Disposition")
                .allowCredentials(true)
                // ✅ 如果你前端需要带 cookie/token 等
                .maxAge(3600);
    }
}


//@Configuration
//public class MvcConfig implements WebMvcConfigurer {
//
//    @Resource
//    private StringRedisTemplate stringRedisTemplate;
//
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // 登录拦截器
//        registry.addInterceptor(new LoginInterceptor())
//                .excludePathPatterns(
//                        "/doc.html",
//                        "/swagger-ui/**",
//                        "/swagger-resources/**",
//                        "/v3/api-docs/**",
//                        "/webjars/**",
//                        "/favicon.ico",
//                        "/error",
//                        "/users/**"
//                ).order(1);
//        // token刷新的拦截器
//        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").excludePathPatterns(
//                "/doc.html",
//                "/swagger-ui/**",
//                "/swagger-resources/**",
//                "/v3/api-docs/**",
//                "/webjars/**",
//                "/favicon.ico",
//                "/error",
//                // 这里也需要放行公共接口
//                "/users/**"
//        ).order(0);
//    }
//}

